﻿using System;
using System.Configuration;
using System.Data;
using NRemedy;
using Wicresoft.EKA.AccountInfoUpdateApp.Model;
using Wicresoft.SelfService.DAO;
using Oracle.DataAccess.Client;
using Wicresoft.EKA.AccountInfoUpdateApp.Service;


namespace Wicresoft.EKA.AccountInfoUpdateApp.Init
{
    public static class UpdateInit
    {
        
        public static bool ChangeUserInfo(AccountInfo model)
        {

            //change account info into the following system
            //1.selfservice
            //2.itportal
            //3.itsm

            Wicresoft.SelfService.BL.ITServiceHelper helper = new Wicresoft.SelfService.BL.ITServiceHelper();
            try
            {
                bool tier1 = helper.UpdateUserAccountInfo(model);
                bool tier2 = updateUerInfoToPortal(model);
                bool tier3 = ITSMUpdateUserAccountInfo(model);

                if (tier1 == false)
                {
                    string msg = "Error in update SelfService : " + model.Account;
                    SysLog.WriteSysLog(msg);
                }
                if (tier2 == false)
                {
                    string msg = "Error in update UnifyShow : " + model.Account;
                    SysLog.WriteSysLog(msg);
                }
                if (tier3 == false)
                {
                    string msg = "Error in update ITSM : " + model.Account;
                    SysLog.WriteSysLog(msg);
                }

                if (tier1 && tier2 && tier3)
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                SysLog.WriteSysLog(e.ToString());
                throw (e);
            }

        }

        private static bool ITSMUpdateUserAccountInfo(AccountInfo model)
        {
            bool ret = false;
            //skip null uid
            if (string.IsNullOrEmpty(model.Account))
                return false;
            var context = new ARLoginContext(ConfigurationManager.AppSettings["ARServer"],
                ConfigurationManager.AppSettings["ARUid"],
                ConfigurationManager.AppSettings["ARPwd"]);
            try
            {
                var _server = new ARProxy<CTMPeople>(context);
                ARProxy<CTMPeople> ctmPeopleARProxy = new ARProxy<CTMPeople>(context);
                string querystr = string.Format("\'4\'=\"{0}\"", model.Account);
                var exitPeople = ctmPeopleARProxy.GetEntryByQuery(querystr);
                if (exitPeople.Count > 0)
                {

                    var source = exitPeople[0];
                    //if (!string.Equals(employee.COMPANY, source.Company)) source.Company = employee.COMPANY;
                    if (!string.Equals(model.Dept, source.Department)) source.Department = model.Dept;
                    //if (!string.Equals(employee.CNAME[0].ToString(), source.FirstName)) source.FirstName = employee.CNAME[0].ToString();
                    //if (!string.Equals(employee.CNAME, source.FullName)) source.FullName = employee.CNAME;
                    //if (!string.Equals(employee.CNAME.Substring(1), source.LastName)) source.LastName = employee.CNAME.Substring(1);
                    if (!string.Equals(model.Phone, source.PhoneNumberBusiness)) source.PhoneNumberBusiness = model.Phone;
                    if (!string.Equals(model.Position, source.JobTitle)) source.JobTitle = model.Position;
                    if (!string.Equals(model.Mail, source.InternetEmail)) source.InternetEmail = model.Mail;

                    context.ServerInstance.SetEntry("CTM:People", source.Request_ID, new ModelBinder<CTMPeople>().UnBindForUpdate(source));

                    //ctmPeopleARProxy.SetEntry(source);
                    ret = true;
                }
            }
            catch
            {
                ret = false;
            }
            finally
            {
                context.Dispose();
            }
            return ret;

        }

        private static bool updateUerInfoToPortal(AccountInfo model)
        {
            if (model == null) return false;
            if (string.IsNullOrEmpty(model.Account)) return false;
            string Sql = "update user_x set department = :dept, position = :position where name = :accountid";
            OracleParameter[] commandParameters = new OracleParameter[]{
                new OracleParameter("dept",model.Dept),
                new OracleParameter("position",model.Position),
                new OracleParameter("accountid",model.Account)
            };

            bool ret = true;
            OracleDataAccess oda = null;
            string conn = ConfigurationManager.ConnectionStrings["ITPortal"].ConnectionString;
            try
            {
                oda = new OracleDataAccess(conn);
                if (oda.ExecuteCommand(Sql, commandParameters) > 0)
                    ret = true;
                else
                    ret = false;
            }
            catch
            {
                ret = false;
            }
            finally
            {
                if (oda != null)
                    oda.Close();
            }
            return ret;
        }

        public static AccountInfo GetAllAccountInfo(string loginId)
        {
            OracleDataAccess oracle = new OracleDataAccess();
            string sql = "select accountid,username,company,dept,position,phone,email from accountcopy where accountid = '" + loginId + "'";
            DataTable dt = oracle.ExecuteFillToDataTable(sql);
            AccountInfo model = new AccountInfo();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    model.Account = dr["accountid"].ToString();
                    model.Company = dr["Company"].ToString();
                    model.Dept = dr["DEPT"].ToString();
                    model.Phone = dr["Phone"].ToString();
                    model.Position = dr["Position"].ToString();
                    break;
                }
            }
            return model;
        }


    }
}
